package com.yunxi.module.system.controller.admin.notify;

import com.yunxi.framework.apilog.core.annotation.ApiAccessLog;
import com.yunxi.framework.common.enums.UserTypeEnum;
import com.yunxi.framework.common.pojo.CommonResult;
import com.yunxi.framework.common.util.object.BeanUtils;
import com.yunxi.framework.security.core.util.SecurityFrameworkUtils;
import com.yunxi.module.system.model.notify.NotifyMessageDO;
import com.yunxi.module.system.service.notify.NotifyMessageService;
import com.yunxi.module.system.vo.admin.notify.message.NotifyMessageRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 管理后台 - 我的站内信
 * </p>
 *
 * @author lidy
 * @since 2024-05-19
 */
@Tag(name = "管理后台 - 我的站内信")
@RestController
@RequestMapping("/system/notifyMessage")
@Validated
public class NotifyMessageController {

    @Resource
    private NotifyMessageService notifyMessageService;

    @GetMapping("/getUnreadList")
    @Operation(summary = "获取当前用户的最新站内信列表，默认 10 条")
    @Parameter(name = "size", description = "10")
    public CommonResult<List<NotifyMessageRespVO>> getUnreadNotifyMessageList(
            @RequestParam(name = "size", defaultValue = "10") Integer size) {
        List<NotifyMessageDO> messageDOS = notifyMessageService.getUnReadNotifyMessageList(
                SecurityFrameworkUtils.getLoginUserId(), UserTypeEnum.ADMIN.getValue(), size);
        return CommonResult.success(BeanUtils.toBean(messageDOS, NotifyMessageRespVO.class));
    }

    @GetMapping("/getUnreadCount")
    @Operation(summary = "获得当前用户的维度站内信数量")
    @ApiAccessLog(enable = false)   // 由于前端会不断轮询该接口，记录日志没有意义
    public CommonResult<Long> getUnreadNotifyMessageCount() {
        return CommonResult.success(notifyMessageService.getUnreadNotifyMessageCount(
                SecurityFrameworkUtils.getLoginUserId(), UserTypeEnum.ADMIN.getValue()));
    }

}
